home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: netcom.com!marnold
- From: marnold@netcom.com (Matt Arnold)
- Subject: Re: How to get at base class fields from member functions?
- Message-ID: <marnoldDoGE1H.FMH@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- References: <internews46B00D1FBD@argonet.co.uk> <4ihhkf$2ni@news5.erols.com>
- Date: Mon, 18 Mar 1996 07:56:05 GMT
- Sender: marnold@netcom4.netcom.com
-
- Chris Cobb <ccobb@erols.com> writes:
-
- >Dave Mullard <dmullard@argonet.co.uk> wrote:
- >>Given three classes A, B and C I want to have multiple As for each B and
- >>multiple Bs for each C. To do this I could create the following.
- >>
- >>class b1 : public B
- >>{
- >>A a1;
- >>A a2;
- >>A a3;
- >>};
- >>
- >>class b2 : public B
- >>{
- >>A a1;
- >>A a2;
- >>A a3;
- >>A a4;
- >>A a5;
- >>};
- >>
- >>class c1 : public C
- >>{
- >>b1 b1;
- >>b2 b2;
-
- Well, as a side-point, the above variable names are illegal---you can't
- name a variable the same name as it's type (as with the two data members
- of c1 above---b1 and b2 need to have different names from their types).
-
- >>};
- >>
- >>The question is, how within functions for class B do I access fields
- >>within class C, and similarly, how within the class A functions do I
- >>access fields within class B?
-
-
- You have to use a somewhat inconvenient "forward declaration" syntax
- so B can refer to types the compiler hasn't seen yet (namely, class C).
- However, you may only use pointers and references to the forward-
- declared types.
-
- C++ is parsed somehwat "linearly"; the compiler does not take
- everything in and then decide if what you coded was legal or not.
- Instead, you must write code with the awareness that, at some points,
- the compiler may not yet understand what you a talking about
- (specifically when it comes to refering to the different classes and
- data types in your program).
-
-
- For example, here is how B might access a data member of C (modelled
- after your sample code)...
-
-
- class C; // this is a forward declaration to a type not yet seen
-
- class B
- {
- public:
- void use_b1_in_C(C& c); // here we refer to the unseen type C
- };
-
- class b1: public B
- {
- public:
- void use_me() { }
- };
-
- class C
- {
- public:
- b1 _b1;
- };
-
- // now the that compiler has seen C (and b1 too), we can write code
- // for B...
-
- void B::use_b1_in_C(C& c)
- {
- c._b1.use_me();
- }
-
- // and use it all like this...
-
- int main()
- {
- B b;
- C c;
-
- b.use_b1_in_C(c); // call's b1::use_me() for _b1 member of C
-
- return 0;
- }
-
-
- Note that public data members are also required here (or you could
- make B a "friend" of both b1 and C if the data members are kept
- private, as you orignally had them) so B can access them.
-
- If you aren't aware, your original code declared all private members,
- since this is the default for a C++ class when you don't specify
- otherwise. However, this was not the main restriction to doing what
- you wanted. The main problem was being able to write code for B in
- terms of C, which requires forward declarations.
-
-
- Regards,
- -------------------------------------------------------------------------
- Matt Arnold | | ||| | |||| | | | || ||
- marnold@netcom.com | | ||| | |||| | | | || ||
- Boston, MA | 0 | ||| | |||| | | | || ||
- 617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
- C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
- -------------------------------------------------------------------------
-